package com.loong.nnm.impl;

import com.loong.nnm.entity.alarm.Alarm;
import com.loong.nnm.entity.alarm.AlarmMaskState;
import com.loong.nnm.entity.alarm.Tca;
import com.loong.nnm.entity.alarm.TcaParameter;
import com.loong.nnm.services.AlarmsServices;
import com.loong.nnm.utils.NetconfProtocolUtils;
import com.loong.nnm.utils.XmlBuilderUtils;
import net.juniper.netconf.XML;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
public class AlarmImpl implements AlarmsServices {

    private static final String PATH_NAME = "xmlns";
    private static final String PATH_VALUE = "urn:ccsa:yang:acc-alarms";
    //@Resource默认按照名字自动装配
//    @Resource
//    private XmlBuilderUtils xmlBuilderUtils;
    private XmlBuilderUtils xmlBuilderUtils;
//    @Resource
    private NetconfToolImpl netconfTool = new NetconfToolImpl();

    private String get(String node, String module) {
        //组装报文，需要查询的子树进行过滤
        XML content = xmlBuilderUtils.getXMlCoat();
        XML me = content.append(module);
        me.setAttribute(PATH_NAME, PATH_VALUE);
        System.out.println(content);
        //下发请求，并返回报文
        return netconfTool.get(node, content);
    }

    @Override
    public String getAlarms(String node) {
        return get(node, "alarms");
    }

    @Override
    public String getAlarm(String node, Alarm alarm) {
        return get(node, "alarms");
    }

    @Override
    public String getAlarm(String node, int alarmSerialNo) {
        return get(node, "alarms");
    }

    @Override
    public String getAlarmMaskState(String node, AlarmMaskState alarmMaskState) {
        return get(node, "alarm-mask-states");
    }

    @Override
    public String getAlarmMaskState(String node) {
        return get(node, "alarm-mask-states");
    }

    @Override
    public String setAlarmMaskState(String node, AlarmMaskState alarmMaskState) {

        netconfTool.reconnection(node);

        StringBuilder context = new StringBuilder();

        context.append("<object-name>").append(alarmMaskState.getObjectName()).append("</object-name>\n");
        context.append("<alarm-code>").append(alarmMaskState.getAlarmCode()).append("</alarm-code>\n");
        context.append("<object-type>").append(alarmMaskState.getObjectType()).append("</object-type>\n");
        context.append("<mask-state>").append(alarmMaskState.isAlarmMaskState()).append("</mask-state>\n");

        String test = "      <edit-config>\n" +
                "  <target>\n" +
                "    <running/>\n" +
                "  </target>\n" +
                "  <config>\n" +
                "    <alarm-mask-states xmlns=\"urn:ccsa:yang:acc-alarms\">\n" +
                "      <alarm-mask-state>\n" +
                context.toString() +
                "      </alarm-mask-state>\n" +
                "    </alarm-mask-states>\n" +
                "  </config>\n" +
                "</edit-config>";
        System.out.println(test);
        return netconfTool.send(node, test);

    }

    @Override
    public String getTcaParameters(String node) {
        return get(node, "tca-parameters");
    }

    @Override
    public String getTcaParameter(String node, TcaParameter tcaParameter) {
        return null;
    }

    @Override
    public String getTca(String node) {
        return get(node, "tcas");
    }

}
